﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Production.ApplicationLib.Command;
using Production.ApplicationLib.Queries;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;

namespace Production.WebApi.Controllers
{
    /// <summary>
    /// 生产资源
    /// </summary>
    [ApiController]
    public class ProductionResourceController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IProductionResourceQueries _productionResourceQueries;


        public ProductionResourceController(IMediator mediator,
            IProductionResourceQueries productionResourceQueries)
        {
            _mediator = mediator;
            _productionResourceQueries = productionResourceQueries;
        }


        [HttpGet(ApiRoutes.ProductionResource.QueryProductionResources)]
        public async Task<IActionResult> Get([FromQuery] ProductionResourceSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _productionResourceQueries.QueryProductionResourcesByPage(model);
                return Ok(data);
            }
            else
            {
                var data = await _productionResourceQueries.QueryProductionResources(model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.ProductionResource.CreateProductionResource)]
        public async Task<IActionResult> Post(CreateProductionResourceCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.ProductionResource.GetProductionResource)]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _productionResourceQueries.GetProductionResource(id);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.ProductionResource.UpdateProductionResource)]
        public async Task<IActionResult> Put(int id, UpdateProductionResourceCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }

    }
}